package edu.cibertec.sicsolutions.parameters;

import java.util.Iterator;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * 
 * 
 */
public final class XMLParameterUtil {
	
	private static final String PATH_PARAMETER_CONFIG = "parameterConfig.xml";
	
	private XMLParameterUtil(){
	}
	
	public static XMLParameterConfig getParameterConfigDefault(){
		return getParameterConfigFromPathRelative(null);
	}
	
	/** Transforma el XML a <code>XMLParameterConfig</code>
	 * @param path
	 * @return
	 */
	public static XMLParameterConfig getParameterConfigFromPathRelative(String path){
		if (path == null) {
			path = PATH_PARAMETER_CONFIG;
		}
		Thread thread = Thread.currentThread();
		ClassLoader cLoader = thread.getContextClassLoader();		
		XStream xstream = inicializeXStream();		
		XMLParameterConfig config = (XMLParameterConfig)xstream.fromXML(cLoader.getResourceAsStream(path));
		System.out.println("JPRU:Frw config.getParameters().size: "+config.getParameters().size());
		return config;
	}
	
	/** Obtiene el valor de la ruta de un XML por defecto
	 * <pre>
	 * XMLParameterUtil.getValue("myKey");
	 * </pre>
	 * @param key
	 * @return
	 */
	public static String getValue(String key){
		return getValuePathRelative(null, key);
	}
	
	/**
	 * @param key
	 * @param path
	 * @return
	 */
	public static String getValuePathRelative(String path, String key){
		if (key == null) {
			return null;
		}
		XMLParameterConfig parameterConfig = getParameterConfigFromPathRelative(path);		
		return getValue(key, parameterConfig);
	}
	
	/** Obtiene el valor de la clase <code>XMLParameterConfig</code>
	 * @param key
	 * @param parameterConfig
	 * @return Puede retornar <code>null</code>
	 */
	public static String getValue(String key, XMLParameterConfig parameterConfig){
		for (Iterator iterator = parameterConfig.getParameters().iterator(); iterator.hasNext();) {
			XMLParameter param = (XMLParameter) iterator.next();
			if (param != null && key.equals(param.getKey())) {
				return param.getValue();
			}						
		}
		return null;
	}
	
	/** 
	 * Incorpora los alias a la configuracion. 
	 * Ejemplo <code>xstream.alias("parameterConfig", XMLParameterConfig.class);</code>
	 * @return
	 */
	private static XStream inicializeXStream(){
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("parameterConfig", XMLParameterConfig.class);
		xstream.alias("parameter", XMLParameter.class);
		return xstream;
	}	
	
	public static XMLParameterConfig getParameterConfig(String xml){
		XStream xstream = inicializeXStream();
		return (XMLParameterConfig)xstream.fromXML(xml);
	}
	
	public static String getValue(String key, String xml){
		return key == null ? null : getValue(key, getParameterConfig(xml));
	}
	
	public static String convertParameterConfigXML(XMLParameterConfig parameterConfig){
		XStream xstream = inicializeXStream();		
		return xstream.toXML(parameterConfig);
	}

}
